package com.aspire.boc.mms.wmf.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author shining
 * @version 2007-7-31 15:57:07
 */
public class CookieUtil {
	private static Log logger = LogFactory.getLog(CookieUtil.class);

	public CookieUtil() {
		super();
	}

	public static String getCookieValue(HttpServletRequest request, String name) {
		Map map = getCookies(request);
		return (String) map.get(name);
	}

	@SuppressWarnings("unchecked")
	public static Map getCookies(HttpServletRequest request) {
		HashMap map = new HashMap();
		Cookie[] cookies = request.getCookies();
		for (int i = 0; cookies != null && i < cookies.length; i++) {
			// logger.info("cookies--" + cookies[i]);
			String name = cookies[i].getName();
			String value = cookies[i].getValue();
			map.put(name, value);
			logger.info("cookies[" + i + "]--" + name + "---" + value);
		}
		return map;
	}

	public static void setCookieValue(HttpServletResponse response,
			String name, String value) {
		logger.info("setCookieValue--" + name + "-----" + value);
		Cookie cookie = new Cookie(name, value);
		response.addCookie(cookie);
	}

	public static void removeCookie(HttpServletResponse response, String name) {
		Cookie cookie = new Cookie(name, "");
		cookie.setMaxAge(0);
		response.addCookie(cookie);
	}

	public static void remove(HttpServletResponse response, String name) {
		remove(response, "", "/", name);
	}

	public static void remove(HttpServletResponse response, String domain,
			String name) {
		remove(response, domain, "/", name);
	}

	public static void remove(HttpServletResponse response, String domain,
			String path, String name) {
		set(response, domain, path, name, "", 0);
	}

	public static void set(HttpServletResponse response, String name,
			String value) {
		// set(response, "", "/", name, value);
		Cookie cookie = new Cookie(name, value);
		logger.info("cookie---" + cookie);
		response.addCookie(cookie);
	}

	public static void set(HttpServletResponse response, String name,
			String value, int age) {
		set(response, "", "/", name, value, age);
	}

	public static void set(HttpServletResponse response, String domain,
			String name, String value, int age) {
		set(response, domain, "/", name, value, age);
	}

	public static void set(HttpServletResponse response, String domain,
			String name, String value) {
		set(response, domain, "/", name, value);
	}

	public static void set(HttpServletResponse response, String domain,
			String path, String name, String value) {
		set(response, domain, path, name, value, -1);
	}

	public static void set(HttpServletResponse response, String domain,
			String path, String name, String value, int age) {

		Cookie cookie = new Cookie(name, value);
		cookie.setDomain(domain);
		cookie.setPath(path);
		cookie.setMaxAge(age);
		logger.info("cookie---" + cookie);
		response.addCookie(cookie);
	}
}
